package org.lql.algo.codecrush.hot100.matrix;

/**
 * @author: liangqinglong
 * @date: 2025-10-11 17:38
 * @description: 240. 搜索二维矩阵 II <a href="https://leetcode.cn/problems/search-a-2d-matrix-ii/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class SearchMatrix {

	public boolean searchMatrix(int[][] matrix, int target) {
		int m = matrix.length;
		int n = matrix[0].length;
		int i = 0;
		int j = n - 1;
		while(i < m && j >= 0) {
			if (matrix[i][j] == target) {
				return true;
			} else if (matrix[i][j] > target) {
				j--;
			} else {
				i++;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		int[][] matrix = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
		SearchMatrix searchMatrix = new SearchMatrix();
		System.out.println(searchMatrix.searchMatrix(matrix, 5));
		System.out.println(searchMatrix.searchMatrix(matrix, 20));
	}
}
